package com.situ.ssm.controller;

import java.util.Arrays;
import java.util.List;

import org.junit.runner.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.fabric.Response;
import com.mysql.fabric.xmlrpc.base.Array;
import com.situ.ssm.common.ServerResponse;
import com.situ.ssm.entity.Student;
import com.situ.ssm.service.IStudentService;

@Controller
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private IStudentService studentService;
	
//	@RequestMapping("/list")
//	@ResponseBody
//	public List<Student>list(){
//		return studentService.list();
//	}
	
//	@RequestMapping("/list")
//	public String list(Model model) {
//		List<Student>list = studentService.list();
//		model.addAttribute("list", list);
//		return "student_list";
//	}
	
	@RequestMapping("/getStudentListPage")
	public String getStudentListPage() {
		return "student_list";
	}
	
	// /student/pageList.action?page=1&limit=30
	@RequestMapping("/pageList")
	@ResponseBody
	public ServerResponse pageList(Integer page, Integer limit, Student student) {
		return studentService.pageList(page,limit, student);
	}
	
	@RequestMapping("/deleteById")
	@ResponseBody
	public ServerResponse deleteById(Integer id) {
		return studentService.deleteById(id);
	}
	
	@RequestMapping("/deleteAll")
	@ResponseBody
	public ServerResponse deleteAll(Integer[] ids) {
		System.out.println(Arrays.toString(ids));
		return studentService.deleteAll(ids);
	}
	
	@RequestMapping("/insert")
	@ResponseBody
	public ServerResponse insert(Student student) {
		System.out.println("StudentController.insert(Student student)");
		return studentService.insert(student);
	}
	
	@RequestMapping("/getInsertPage")
	public String getInsertPage() {
		System.out.println("StudentController.getInsertPage(Integer id)");
		return "student_insert";
	}
	
	@RequestMapping("/toUpdate")
	public String toUpdate(Integer id, Model model) {
		System.out.println("StudentController.toUpdate()");
		Student student = studentService.selectById(id);
		
		//开辟一块内存存放student
		model.addAttribute("student", student);
		return "student_update";
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ServerResponse update(Student student) {
		System.out.println("StudentController.update()");
		return studentService.update(student);
	}
	
}
